console.log(`content_script: ${new Date().getTime()}`)

async function sleep( sec=1){
    milisec = sec * 1000;
    return new Promise ( resolve => {
        setTimeout(resolve,milisec);
        console.log(`slept for ${sec} seconds` );

    }
    );

}


const StartTimelapse = new Date().getTime();
let btn ;
async function login(
   user,
   password,
   client_id,
   client_secret,
   password_type,
   autoLoginClick=true

) {
    loginBtn = document.querySelector(".auth-wrapper button");
    while (typeof (loginBtn) == "undefined" || loginBtn === null)
    {
        await sleep(0.01);
        loginBtn = document.querySelector(".auth-wrapper button");

    }
    if (typeof (loginBtn) != "undefined" && loginBtn != null) {
        btn = loginBtn;
        if (btn.classList.contains("unlocked").toString()) {
            btn.click();
            const evt = document.createEvent("HTMLEvents");
            evt.initEvent('change', true, true)
            let input_oauth_username = document.querySelector('input#oauth_username')
            let input_oauth_password = document.querySelector('input#oauth_password')
            let select_password_type = document.querySelector('select#password_type')
            let input_client_id = document.querySelector('input#client_id')
            let input_client_secret = document.querySelector('input#client_secret')
            for (let i=0;i<select_password_type.length;i++)
            {
                if (select_password_type[i].value.toLowerCase() === password_type.toLowerCase()){
                    select_password_type.selectedIndex = i
                    select_password_type.dispatchEvent(evt);
                }
            }
            input_oauth_username.value = user;
            input_oauth_username.dispatchEvent(evt);

            input_oauth_password.value = password;
            input_oauth_password.dispatchEvent(evt);

            input_client_id.value = client_id;
            input_client_id.dispatchEvent(evt);

            
            input_client_secret.value = client_secret;
            input_client_secret.dispatchEvent(evt);

            

            
            
        }

        document.querySelector(".auth-btn-wrapper button.btn.modal-btn.auth.authorize.button").click();
        // xbtn = document.evaluate("//button[contains(.,'Close')]",
        //     document, null, XPathResult.ANY_TYPE, null);
        // closeBtn = xbtn.iterateNext();
        // while (closeBtn === null) {
        //     console.log("not found")
        //     await sleep(0.01);
        //     xbtn = document.evaluate("//button[contains(.,'Close')]",
        //     document, null, XPathResult.ANY_TYPE, null);
        //     closeBtn = xbtn.iterateNext();
        // }
        // if(autoLoginClick === true){
        //       closeBtn.click();
        // }

    }
}

browser.storage.local.get().then(item=>{
    setting = item.setting;
    auth = item.auth;
    if(setting.ison){
        login(user=auth.user,
            password=auth.password,
            client_id=auth.client_id,
            client_secret=auth.client_secret,
            password_type = auth.password_type
            )
        }
    
});

//TODO run script after document fully loaded not with timeou
// browser.runtime.sendMessage({ "iNeed": "setting" }).then(message=>console.log(message));
 